public class demo {
    public static void main(String[] args) {
        int[] array ={1,54,76,2,8,43,4};
        int key=8;
        int left=0;
        int right=array.length-1;
        while(left<right){
            int mid = (left+right)/2;
            if(array[mid]>key){
                right=mid-1;
                left++;
            }else if(array[mid]<key){
                left=mid+1;
                right--;
            }else if(array[mid]==key){
                System.out.println(mid);
                break;
            }
        }
    }
}
